/*
 *                        _oo0oo_
 *                       o8888888o
 *                       88" . "88
 *                       (| -_- |)
 *                       0\  =  /0
 *                     ___/`---'\___
 *                   .' \\|     |// '.
 *                  / \\|||  :  |||// \
 *                 / _||||| -:- |||||- \
 *                |   | \\\  - /// |   |
 *                | \_|  ''\---/''  |_/ |
 *                \  .-\__  '-'  ___/-. /
 *              ___'. .'  /--.--\  `. .'___
 *           ."" '<  `.___\_<|>_/___.' >' "".
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /
 *      =====`-.____`.___ \_____/___.-`___.-'=====
 *                        `=---='
 * 
 * 
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *            佛祖保佑     永不宕机     永无BUG
 * 
 *        佛曰:  
 *                写字楼里写字间，写字间里程序员；  
 *                程序人员写程序，又拿程序换酒钱。  
 *                酒醒只在网上坐，酒醉还来网下眠；  
 *                酒醉酒醒日复日，网上网下年复年。  
 *                但愿老死电脑间，不愿鞠躬老板前；  
 *                奔驰宝马贵者趣，公交自行程序员。  
 *                别人笑我忒疯癫，我笑自己命太贱；  
 *                不见满街漂亮妹，哪个归得程序员？
 */

/*
 * @Descripttion: 
 * @version: 
 * @Author: Eugene
 * @Date: 2024-07-04 15:42:35
 * @LastEditors: Andy
 * @LastEditTime: 2024-07-07 11:27:00
 */

#include "WHW_IRQN.h"
#include "All_Init.h"

uint8_t aaa;
float t1,t2,dt;
static uint8_t TX[12] = {0xff,0xf1,0xfd,0x90,0x86,0xa7,0xff,0xf1,0xfd,0x90,0x86,0xa7};

void StartChassisTask(void const * argument)
{
    portTickType currentTimeChassis;
    currentTimeChassis = xTaskGetTickCount();

	Motor_Init();
	for(;;)
	{
        t1 = DWT_GetTimeline_ms();

        t2 = DWT_GetTimeline_ms();
        dt = (t2 - t1);

        vTaskDelayUntil(&currentTimeChassis, 1);
	}
}

void StartRootTask(void const * argument)
{
    portTickType currentTimeRoot;
    currentTimeRoot = xTaskGetTickCount();

    for(;;)
    {
        RUI_F_ROOT(&RUI_ROOT_STATUS, &Motors);

        vTaskDelayUntil(&currentTimeRoot, 5);
    }
}

void USER_TIM_IRQHandler(TIM_HandleTypeDef *htim)
{

}

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{
    FDCAN_RxHeaderTypeDef RxHeader1;
    uint8_t g_Can1RxData[64];
    if((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET)
    {
        if(hfdcan->Instance == FDCAN1)
        {
            /* Retrieve Rx messages from RX FIFO0 */
            memset(g_Can1RxData, 0, sizeof(g_Can1RxData));	//接收前先清空数组
            HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &RxHeader1, g_Can1RxData);
            switch(RxHeader1.Identifier)
            {
                case 0x201:
                    RUI_F_MOTOR_CAN_RX_3508RM(&DJI_MOTOR_Pitch.DATA, g_Can1RxData);
                    break;
            }
        }
    }
}

void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs)
{
    FDCAN_RxHeaderTypeDef RxHeader2;
    uint8_t g_Can2RxData[64];

    FDCAN_RxHeaderTypeDef RxHeader3;
    uint8_t g_Can3RxData[64];

    if((RxFifo1ITs & FDCAN_IT_RX_FIFO1_NEW_MESSAGE) != RESET)
    {
        if(hfdcan->Instance == FDCAN2)
        {
            /* Retrieve Rx messages from RX FIFO0 */
            memset(g_Can2RxData, 0, sizeof(g_Can2RxData));
            HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO1, &RxHeader2, g_Can2RxData);
            switch(RxHeader2.Identifier)
            {
                case 0x202:
                    RUI_F_MOTOR_CAN_RX_2006RM(&DJI_MOTOR_Roll.DATA, g_Can2RxData);
                    break;
            }
        }

        if(hfdcan->Instance == FDCAN3)
        {
            /* Retrieve Rx messages from RX FIFO0 */
            memset(g_Can3RxData, 0, sizeof(g_Can3RxData));
            HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO1, &RxHeader3, g_Can2RxData);
            switch(RxHeader3.Identifier)
            {

            }
        }
    }
}

#define  BUFFER_SIZE_4  (255)
extern DMA_HandleTypeDef hdma_uart4_rx;

void USER_UART_IRQHandler(UART_HandleTypeDef *huart)
{
    if(huart->Instance ==USART2)//遥控接收
    {
        if (__HAL_UART_GET_FLAG(&huart2 , UART_FLAG_IDLE) == 1) {

            RUI_F_DUBS_Resovled(DBUS_RX_DATA, &WHW_V_DBUS);
            
            __HAL_UART_CLEAR_IDLEFLAG(&huart2);
            HAL_UART_DMAStop(&huart2);
            HAL_UART_Receive_DMA(&huart2, DBUS_RX_DATA, 19);
        }
    }

    if(huart->Instance ==UART4)//裁判系统
    {
        if(__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE) != RESET)   //判断是否是空闲中断
        {
            uint8_t data_length_4;

            data_length_4  = BUFFER_SIZE_4 - __HAL_DMA_GET_COUNTER(&hdma_uart4_rx);//计算接收到的数据长度
            Read_Data_first(&ALL_RX , &User_data , data_length_4);//测试函数：待修改
            memset((uint8_t*)ALL_RX.Data,0,data_length_4);//清零接收缓冲区

            __HAL_UART_CLEAR_IDLEFLAG(&huart4);//清楚空闲中断标志（否则会一直不断进入中断）
            HAL_UART_DMAStop(&huart4);//停止本次DMA传输
            HAL_UART_Receive_DMA(&huart4, (uint8_t*)ALL_RX.Data, BUFFER_SIZE_4);
        }
    }

    if(huart->Instance ==USART1)//调试使用
    {
        if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET)   //判断是否是空闲中断
        {
            //数据处理

            __HAL_UART_CLEAR_IDLEFLAG(&huart1);//清楚空闲中断标志（否则会一直不断进入中断）
            HAL_UART_DMAStop(&huart1);//停止本次DMA传输
            HAL_UART_Receive_DMA(&huart1, (uint8_t *)RX, 20);
        }
    }
}
